package models;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import util.DBAction;
import util.MyLogAction;

public class TagData {

	private int tagID;
	private String tagName;
	private int issueId;
	public int getIssueId() {
		return issueId;
	}
	public void setIssueId(int issueId) {
		this.issueId = issueId;
	}
	public int getTagID() {
		return tagID;
	}
	public void setTagID(int tagID) {
		this.tagID = tagID;
	}
	public String getTagName() {
		return tagName;
	}
	public void setTagName(String tagName) {
		this.tagName = tagName;
	}
	
	
	public int update() {
		String updateSQL = 
			    "update tags "
			    + "set tag_name = '" + tagName + 
				"where tag_id = " + tagID;
		return DBAction.update(updateSQL);
	}

	public int insert() {
		String insertSQL = "insert into tags "
				+ "(tag_name,issue_id) " + "values('" + tagName +"',"+ issueId +");";
		return DBAction.update(insertSQL);
	}
	
	public int delete(int issueid) {
		System.out.println("in tagdata with iid"+issueid);
		String deleteSQL = "delete from tags "
				+ "where issue_id = " + issueid;
		
		
		System.out.println("delete sql is"+deleteSQL);
		return DBAction.update(deleteSQL);
	}
	
	public static ArrayList<TagData> find(String selectionModifier) {
		ArrayList<TagData> tagSelection = new ArrayList<TagData>();
		ResultSet resultSet = null;
		String query = "select * "+
				"from tags " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				TagData tag = new TagData();
				tag.tagID=resultSet.getInt("tag_id");
				tag.tagName=resultSet.getString("tag_name");
				tag.issueId=resultSet.getInt("issue_id");
				tagSelection.add(tag);
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("TagData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return tagSelection;
	}
	public static ArrayList<TagData> tag_name_unique(String selectionModifier) {
		ArrayList<TagData> tagSelection = new ArrayList<TagData>();
		ResultSet resultSet = null;
		String query = "select distinct(tag_name) "+
				"from tags " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				TagData tag = new TagData();
				//tag.tagID=resultSet.getInt("tag_id");
				tag.tagName=resultSet.getString("tag_name");
				//tag.issueId=resultSet.getInt("issue_id");
				tagSelection.add(tag);
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("TagData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return tagSelection;
	}
	public static ArrayList<TagData> Issue_id_unique(String selectionModifier) {
		ArrayList<TagData> tagSelection = new ArrayList<TagData>();
		ResultSet resultSet = null;
		String query = "select distinct(issue_id) "+
				"from tags " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				TagData tag = new TagData();
				//tag.tagID=resultSet.getInt("tag_id");
				//tag.tagName=resultSet.getString("tag_name");
				tag.issueId=resultSet.getInt("issue_id");
				tagSelection.add(tag);
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("TagData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return tagSelection;
	}

	
	public static TagData findOne(String selectionModifier) {
		ResultSet resultSet = null;
		String query = "select * "+
				"from tags " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			if (resultSet.next()) {
				TagData tag1 = new TagData();
				tag1.tagID = resultSet.getInt("tag_id");
				tag1.tagName = resultSet.getString("tag_name");
				DBAction.close(resultSet);
				DBAction.close(connection);
				return tag1;
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("TagData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return null;
	}

	public static ArrayList<TagData> Tag_name(String selectionModifier) {
		ArrayList<TagData> tagSelection = new ArrayList<TagData>();
		ResultSet resultSet = null;
		String query ="select tag_name from tags " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				TagData tag = new TagData();
				
				tag.tagName=resultSet.getString("tag_name");
				tagSelection.add(tag);
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("TagData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return tagSelection;
	
		
	}
	
}
